import moment from 'moment'

const vFilter = {}
vFilter.install = function (Vue) {
// 证件号码过滤器
Vue.filter('getEncryCard', function (idCard) {
  if (!idCard) {
    return ''
  }
  // 保证第二位和倒数第二位开始是* 就返回原字符串不过滤
  if (idCard.startsWith('*', 2) && idCard.endsWith('*', idCard.length - 2)) {
    return idCard
  }
  let credentialNo = idCard.substring(0, 2)
  let encry = idCard.substring(2, idCard.length - 1)
  // eslint-disable-next-line
  for (let i of encry) {
    credentialNo += '*'
  }
  credentialNo += idCard.substring(idCard.length - 2, idCard.length)
  return credentialNo
})

// 手机号码过滤器
Vue.filter('getEncryPhone', function (phone) {
  if (!phone) {
    return ''
  }
  // 保证第二位和倒数第二位开始是* 就返回原字符串不过滤
  if (phone.startsWith('*', 3) && phone.endsWith('*', phone.length - 2)) {
    return phone
  }
  let number = phone.substring(0, 3)
  let encry = phone.substring(3, phone.length - 1)
  if (encry) {
    // eslint-disable-next-line
    for (let i of encry) {
      number += '*'
    }
  }
  number += phone.substring(phone.length - 2, phone.length)
  return number
})

// 过滤时间格式 2018-06-08 => 06-08
Vue.filter('filterDateFormat', (date) => {
  if (!date) return ''
  return moment(date).format('MM-DD')
})

// 过滤时间格式 2018-06-08 => 周五
Vue.filter('filterWeekFormat', (date) => {
  if (!date) return ''
  let week = parseInt(moment(date).day())
  switch (week) {
    case 0:
      return '周日'
    case 1:
      return '周一'
    case 2:
      return '周二'
    case 3:
      return '周三'
    case 4:
      return '周四'
    case 5:
      return '周五'
    case 6:
      return '周六'
    default:
      return '周'
  }
})

// 机场名称过滤 去掉国际机场四个字
Vue.filter('filterFlightName', (flightName) => {
  if (!flightName) return ''
  return flightName.replace('机场', '').replace('国际', '')
})

// 机场名称过滤 去掉国际两个字
Vue.filter('filterIntFlightName', (flightName) => {
  if (!flightName) return ''
  return flightName.replace('国际', '')
})

// 航班列表时间格式转换
Vue.filter('filterFlightDate', (flightDate) => {
  if (!flightDate) return ''
  return moment(flightDate).format('HH:mm')
})
Vue.filter('filterDate', (flightDate) => {
  if (!flightDate) return ''
  return moment(flightDate).format('Y-MM-DD')
})

// 今明后三天显示过滤
Vue.filter('switchWeek', dates => {
  let currentDate = moment().format('Y-MM-DD')
  const week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  const TODAY = '今天'
  const TOMORROW = '明天'
  const AFTER_TOMORROW = '后天'

  let date = moment(dates)
  let curTime = +moment(currentDate)
  let datesTime = +moment(dates)
  if (curTime === datesTime) {
    return TODAY
  } else if (datesTime <= curTime + 86400000) {
    return TOMORROW
  } else if (datesTime <= curTime + 86400000 + 86400000) {
    return AFTER_TOMORROW
  } else {
    return week[date.day()]
  }
})

// 城市名称超过五个中文字符...显示
Vue.filter('switchCityName', name => {
  if (!name) return name
  let length = name.length
  if (length < 4) return name
  if (length >= 4) return `${name.substr(0, 4)}...`
})

Vue.filter('switchCityName8', name => {
  if (!name) return name
  let length = name.length
  if (length < 8) return name
  if (length >= 9) return `${name.substr(0, 9)}...`
})
}

export default vFilter